package com.wangx1n.boobey.util;

import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangxin
 * @create 2020-04-25 14:06
 */
public class WxTool {
    public static final String appid="wx1bc0de41813b6547";
    public static final String secret="f6d8fdf90f5e24281db4497d20f8e7f3";
    public static final String grant_type="authorization_code";
    /**
     * 登录调用获取用户的openid和session_key
     * @param code
     */
    public static String code2Session(String code) {
        final String uri = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}";
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", appid);
        params.put("secret", secret);
        params.put("js_code", code);
        params.put("grant_type", grant_type);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(uri, String.class, params);
        System.out.println(result);
        return result;
    }
}
